define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consumable/consumable/index' + location.search,
                    add_url: 'consumable/consumable/add',
                    edit_url: 'consumable/consumable/edit',
                    del_url: 'consumable/consumable/del',
                    multi_url: 'consumable/consumable/multi',
                    import_url: 'consumable/consumable/import',
                    resubmit_url: 'consumable/consumable/resubmit',
                    dragsort_url: "",
                    table: 'consumable',
                }
            });

            var table = $("#table");
            
            $(".btn-add").data("area", ["100%", "100%"]);

            $(document).on("click", ".goods-shop-type-item", function () {
                $(".active-type").removeClass('active-type');
                $(this).addClass('active-type');
                window.location =  $(this).data('url');
            });

            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                    $(".btn-editone").data("area", ["100%", "100%"]);
             });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh desc,id desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                Search:true,
                commonSearch:true,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                       // {field: 'shop_id', title: __('Shop_id')},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'unit', title: __('Unit'), operate: 'LIKE'},
                       // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                       // {field: 'costprice', title: __('Costprice'), operate:'BETWEEN'},
                      //  {field: 'channelprice', title: __('Channelprice'), operate:'BETWEEN'},
                        
                        
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'inventory', title: __('Inventory')},
                        {field: 'forewarning', title: __('Forewarning')},
                       // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                       // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name:'pass',//名称
                                    text:'下架',
                                    title:'下架',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    icon: 'fa',//图标 可在添加菜单规则处 搜索图标见示例图
                                    url: 'consumable/consumable/delist',//接口地址 控制器名/
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.status == 0){
                                            return false;//显示
                                        }else{
                                            return true;//隐藏
                                        }
                                    },
                                    refresh:true
                                },
                                {
                                    name:'pass',//名称
                                    text:'上架',
                                    title:'上架',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    icon: 'fa',//图标 可在添加菜单规则处 搜索图标见示例图
                                    url: 'consumable/consumable/listing',//接口地址 控制器名/
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.status == 1){
                                        return false;//显示
                                        }else{
                                            return true;//隐藏
                                        }
                                    },
                                    refresh:true
                                },
                                {
                                    name: 'resubmit',
                                    text: '铺设',
                                    title: '铺设',
                                    icon: '',
                                    extend: 'data-area=\'["80%", "80%"]\'',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: "consumable/consumable/resubmit",
                                    hidden:function(row){
                                        if(row.show_copy == 0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'consumable/consumable/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'consumable/consumable/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'consumable/consumable/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        resubmit: function () {
            var goods_id = Config.goods_id;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consumable/consumable/resubmit?ids=' + goods_id +"&"+ location.search,
                    // table: 'wanlshop_shop',
                }
            });

            var table = $("#table");

            $(".btn-add").data("area", ["100%", "100%"]);

            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                    $(".btn-editone").data("area", ["100%", "100%"]);
             });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                // sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),visible:false, operate: false},
                        {field: 'code', title: __('店铺编号'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('商家分类'), searchList: {"0":__('直营店'),"1":__('加盟店'),"2":__('社区单店')}, operate: false,formatter: Table.api.formatter.normal},
                        {field: 'shopname', title: __('商家名称'), operate: 'LIKE'},
                        {field: 'person', title: __('联系人'), operate: 'LIKE', operate: false},
                        {field: 'telephone', title: __('联系方式'), operate: 'LIKE', operate: false},
                        {field: 'city', title: __('所属地区'), operate: 'LIKE', operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('营业中'),"hidden":__('下线')}, formatter: Table.api.formatter.status, operate: false}
                    ]
                ]
            });

            $("button[type='submit']").css("background-color","red");
            $("button[type='submit']").css("border-color","red");

            //保存设置
            $(document).on("click", ".choose-shop", function () {
                var ids = Table.api.selectedids(table);
                if (ids.length == 0) {
                    layer.msg("请选择要铺设的店铺！");
                }
                Fast.api.ajax({
                    url: 'consumable/consumable/savegoodsshop',
                    type: 'post',
                    loading: false,
                    data: {
                        ids: ids,
                        goods_id: goods_id
                    }
                }, function (data, ret) {
                    if (ret.code == 1) {
                        Layer.open({
                            title:'提示',
                            type: 1, 
                            area: ['280px', '180px'],
                            content: '<p style="padding: 15px;font-size: 16px;color: #ff0000;">铺设成功</p>',
                            btn: ['确定'], // 弹窗按钮
                            btnAlign:"c",
                            yes: function(index, layero){
                                // 点击确定按钮后的回调函数
                                // index是弹窗的索引，layero是弹窗的DOM对象
                                // Layer.close(index);
                                parent.Layer.closeAll();
                                parent.$("a.btn-refresh").trigger("click");
                            }
                        });
                    }else{
                        Layer.open({
                            title:'提示',
                            type: 1, 
                            area: ['280px', '180px'],
                            content: '<p style="padding: 15px;font-size: 16px;color: #ff0000;">操作失败，请售后重试！</p>',
                            btn: ['确定'], // 弹窗按钮
                            btnAlign:"c",
                            yes: function(index, layero){
                                // 点击确定按钮后的回调函数
                                // index是弹窗的索引，layero是弹窗的DOM对象
                                // Layer.close(index);
                                parent.Layer.closeAll();
                                parent.$("a.btn-refresh").trigger("click");
                            }
                        });
                    }
                    return false;
                });
            });
            
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        topgoods: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consumable/consumable/topgoods' + location.search,
                    add_url: 'consumable/consumable/add',
                   // edit_url: 'wanlshop/goods/edit',
                   // del_url: 'wanlshop/goods/del',
                    multi_url: 'consumable/consumable/multi',
                    resubmit_url: 'consumable/consumable/resubmit',
                    dragsort_url: "",
                    table: 'consumable',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);

            $(document).on("click", ".goods-shop-type-item", function () {
                $(".active-type").removeClass('active-type');
                $(this).addClass('active-type');
                window.location =  $(this).data('url');
            });
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                    $(".btn-editone").data("area", ["100%", "100%"]);
             });

            table.on('post-common-search.bs.table', function (event, table) {
                $('ul.nav-tabs li a[data-value="normal"]').trigger('click');
                $(".form-commonsearch select[name=status]").val("normal");
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                Search:true,
                commonSearch:true,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id',title: __('Id') ,operate: false,visible:false},
                        {field: 'code',title: __('Code') ,operate: 'like'},
                        {field: 'name',title: __('Name'),operate: 'like'},
                        {field: 'price',title: __('价格(元)'),operate: false},
                        {field: 'inventory', title: __('Inventory'),operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons :[
                                {
                                    name: 'detail',
                                    text: '耗材详情',
                                    title: '耗材详情',
                                    icon: '',
                                    extend: 'data-area=\'["80%", "80%"]\'',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: "consumable/consumable/detail",
                                },
                                {
                                    name: 'resubmittoshop',
                                    text: '添加到本店',
                                    title: '添加到本店',
                                    icon: '',
                                    extend: 'data-area=\'["80%", "80%"]\'',
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    url: "consumable/consumable/resubmittoshop",
                                    refresh: true,
                                    hidden:function(row){
                                        if(row.already_copy == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                },
                            ],
                            callback: function (data) {
                                table.bootstrapTable('refresh', {});
                                return false;
                            },
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function() {
            $("input").attr('disabled',true);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
